<?php
abstract class Validate_Abstract
{
	public function numeric($value) {
		return (is_numeric($value));
	}
	
	public function required($value) {
		return !empty($value);
	}
	
	public function maxlen($value, $params) {
		return (strlen($value)) <= (int)$params[0];
	}
	
	public function minlen($value, $params) {
		return (strlen($value)) >= (int)$params[0];
	}
	
	public function email($value) {
		return preg_match(
			'/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{1,3})+$/',
			$value
		);
	}
	
	public function cpf($value)
	{
		// Get just numbers
		$value = (string) preg_replace('/\D+/', '', $value);
		$len = strlen($value);
		
		// If have no 11 digits then is a invalid CPF
		if($len != 11) {
			return false;
		}
		
		// If all numbers are the same
		$diff = array();
		for($i=0;$i<11;$i++) {
			if(!in_array($value[$i], $diff)) {
				$diff[] = $value[$i];
			}
		}
		if(count($diff) == 1) {
			return false;
		}
		
		// Get the 10 firsts chars
		$valueC = substr($value, 0, 9);
		
		// First logic
		$sum = 0;
		for($p = 0, $i=10; $i>=2; $p++, $i--) {
			$sum += ($valueC[$p] * $i);
		}
		$rest = $sum%11;
		
		// Add found number
		$valueC .= ($rest < 2) ? '0' : (11-$rest);
		
		// Second logic
		$sum = 0;
		for($p = 0, $i=11; $i>=2; $p++, $i--) {
			$sum += ($valueC[$p] * $i);
		}
		$rest = $sum%11;
		
		// Add last found number
		$valueC .= ($rest < 2) ? '0' : (11-$rest);
		
		// Return a bool of comparison of the number passed and the number found
		return ($valueC === $value);
	}
	
	public function cnpj($value)
	{
		// Get just numbers
		$value = (string) preg_replace('/\D+/', '', $value);
		$len = strlen($value);
		
		// If have no 11 digits then is a invalid CPF
		if($len != 14) {
			return false;
		}
		
		// If all numbers are the same
		$diff = array();
		for($i=0;$i<14;$i++) {
			if(!in_array($value[$i], $diff)) {
				$diff[] = $value[$i];
			}
		}
		if(count($diff) == 1) {
			return false;
		}
		
		// First logic
		$sum = 0;
		
		// Part 1
		$valueC = substr($value, 0, 4);
		for($p = 0, $i=5; $i>=2; $p++, $i--) {
			$sum += ($valueC[$p] * $i);
		}
		
		// Part 2
		$valueC = substr($value, 4, 8);
		for($p = 0, $i=9; $i>=2; $p++, $i--) {
			$sum += ($valueC[$p] * $i);
		}
		
		$rest = $sum%11;
		
		// Get 12 firsts
		$valueC = substr($value, 0, 12);
		
		// Add found number
		$valueC .= ($rest < 2) ? '0' : (11-$rest);
		
		// Reset var
		$sum = 0;
		
		// Second logic - PART 1
		$valueC2 = substr($valueC, 0, 5);
		for($p = 0, $i=6; $i>=2; $p++, $i--) {
			$sum += ($valueC2[$p] * $i);
		}
		// PART 2
		$valueC2 = substr($valueC, 5, 8);
		for($p = 0, $i=9; $i>=2; $p++, $i--) {
			$sum += ($valueC2[$p] * $i);
		}
		$rest = $sum%11;
		
		// Add last found number
		$valueC .= ($rest < 2) ? '0' : (11-$rest);
		
		// Return a bool of comparison of the number passed and the number found
		return ($valueC === $value);
	}
}